package com.cy.detectsys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cy.detectsys.entity.DbWiki;
import com.cy.detectsys.mapper.DbTargetSubdomainMapper;
import com.cy.detectsys.mapper.DbWidePingMapper;
import com.cy.detectsys.service.DbWikiService;
import com.cy.detectsys.mapper.DbWikiMapper;
import com.cy.detectsys.util.R;
import jakarta.annotation.Resource;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author admin
 * @description 针对表【db_wiki】的数据库操作Service实现
 * @createDate 2024-05-10 19:35:59
 */
@Service
public class DbWikiServiceImpl extends ServiceImpl<DbWikiMapper, DbWiki>
        implements DbWikiService {


    @Resource
    private DbTargetSubdomainMapper targetSubdomainMapper;
    @Resource
    private DbWidePingMapper widePingMapper;

    @Override
    public R queryTargetDetails(String domain) {

        QueryWrapper<DbWiki> wikiQueryWrapper = new QueryWrapper<>();
        wikiQueryWrapper.eq("domain", domain);
        wikiQueryWrapper.last("limit 1");
        DbWiki dbWiki = baseMapper.selectOne(wikiQueryWrapper);
        return R.ok(dbWiki);

    }

    /**
     * 查询用户的子域名等其他信息
     *
     * @param domain
     * @return
     */
    @Override
    public R queryTargetSubdomain(String domain) {

        //查询子域名
        List<String> subdomains = targetSubdomainMapper.selectByDomain(domain);


        ArrayList<HashMap> result = new ArrayList<>();


        //查询ip
        for (String subdomain : subdomains) {


            String ip = widePingMapper.selectIpbySubdomain(subdomain);
//            String ip = "104.18.14.158";
            HashMap<String, Object> item = new HashMap<>();

            item.put("subdomain", subdomain);
            if (StrUtil.isNotEmpty(ip)) {
                item.put("ip", ip);
                Map<String, Object> ipas = getIpas(ip);
                item.put("asn", ipas);
            }

            result.add(item);
        }

        return R.ok(result);


    }

    @Resource
    private RestHighLevelClient restHighLevelClient;

    private Map<String, Object> getIpas(String ip) {

        SearchRequest searchRequest = new SearchRequest("ipas");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 精确查询，添加查询条件
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("ip.keyword", ip);
        searchSourceBuilder.query(termQueryBuilder);

        // 执行查询
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = null;
        try {
            searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        SearchHits hits = searchResponse.getHits();
        Map<String, Object> sourceAsMap = new HashMap<>();
        for (SearchHit documentFields : hits.getHits()) {
            sourceAsMap = documentFields.getSourceAsMap();
        }
        return sourceAsMap;
    }
}




